package org.jeecg.modules.friends.pics;

import java.util.List;

import org.jeecg.modules.friends.condition.UserPicsCondition;
import org.jeecg.modules.friends.condition.UserPicsCondition.Criteria;
import org.jeecg.modules.friends.mapper.ext.UserPicsDOExtMapper;
import org.jeecg.modules.friends.model.UserPicsDO;
import org.jeecg.modules.friends.momentspics.entity.MomentsPics;
import org.jeecg.modules.friends.momentspics.mapper.MomentsPicsMapper;
import org.jeecg.modules.friends.pics.dto.ImageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2025-01-04
 */
@Slf4j
@RestController
@RequestMapping("/aliyun")
@Scope("prototype")
public class AliyunCallBackController {

    @Autowired
    private UserPicsDOExtMapper userPicsDOExtMapper;
    @Autowired
    private MomentsPicsMapper momentsPicsMapper;
    private final static String defaultUrl = "https://friends-images.oss-cn-hangzhou.aliyuncs.com/static/weigui.png";

    @RequestMapping(value = "/image/callback", method = {RequestMethod.POST})
    public String aliyunCallback(@RequestParam String checksum, @RequestParam String content) {
        log.info("aliyunCallback ,checksum:{},content:{}", checksum, content);

        if (StrUtil.isBlank(content)) {
            return "success";
        }
        ImageResultDTO imageResultDTO = JSON.parseObject(content, ImageResultDTO.class);
        if (imageResultDTO == null) {
            return "success";
        }
        if (!imageResultDTO.getFreezed()) {
            return "success";
        }
        String picUrl = "https://friends-images.oss-cn-hangzhou.aliyuncs.com/" + imageResultDTO.getObject();
        UserPicsCondition condition = new UserPicsCondition();
        condition.createCriteria()
                .andPicUrlEqualTo(picUrl);
        List<UserPicsDO> userPicsDOS = userPicsDOExtMapper.selectByCondition(condition);

        if (CollectionUtil.isNotEmpty(userPicsDOS)) {
            UserPicsDO userPicsDO = userPicsDOS.get(0);
            UserPicsDO updateContent = new UserPicsDO();
            updateContent.setId(userPicsDO.getId());
            updateContent.setPicUrl(defaultUrl);
            userPicsDOExtMapper.updateByPrimaryKeySelective(updateContent);
            return "success";
        }
        LambdaQueryWrapper<MomentsPics> momentsPicsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        momentsPicsLambdaQueryWrapper.eq(MomentsPics::getPicUrl, picUrl);
        List<MomentsPics> momentsPics = momentsPicsMapper.selectList(momentsPicsLambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(momentsPics)) {
            MomentsPics momentsPics1 = momentsPics.get(0);
            momentsPics1.setPicUrl(defaultUrl);
            momentsPicsMapper.updateById(momentsPics1);
            return "success";
        }
        return "success";
    }
}
